<?php
class ComsenzUserService
{
	/*
	 * discuz_auth_key计算方法参考 {discuz_home}/api/uc.php 191行: $discuz_auth_key = md5($_DCACHE['settings']['authkey'].$_SERVER['HTTP_USER_AGENT']);
	 */
	static private function _getDzAuthKey()
	{
		$comsenzUserDao = new ComsenzUserDao();
		$dzSetting = $comsenzUserDao->getDzSetting();
		return md5($dzSetting['authkey'] . $_SERVER['HTTP_USER_AGENT']);
	}
	
	static private function _getUchAuthKey()
	{
		preg_match('/define\(\'UC_KEY\',\s*\'(.*)\'\)/', file_get_contents(Config::$app['comsenz']['uch_config_file']), $match);
		return $match[1];		
	}
	
	static private function _getDzCookiePre()
	{
		preg_match('/\$cookiepre\s*=\s*\'(.*)\'/', file_get_contents(Config::$app['comsenz']['dz_config_file']), $match);
		return $match[1];
	}
	
	static private function _getUchCookiePre()
	{
		preg_match('/\$_SC\[\'cookiepre\'\]\s*=\s*\'(.*)\'/', file_get_contents(Config::$app['comsenz']['uch_config_file']), $match);
		return $match[1];
	}
	
	static private function _getDzCookieDomain()
	{
		preg_match('/\$cookiedomain\s*=\s*\'(.*)\'/', file_get_contents(Config::$app['comsenz']['dz_config_file']), $match);
		return $match[1];
	}
	
	static private function _getUchCookieDomain()
	{
		preg_match('/\$_SC\[\'cookiedomain\'\]\s*=\s*\'(.*)\'/', file_get_contents(Config::$app['comsenz']['uch_config_file']), $match);
		return $match[1];
	}

	static function setAuthCookie($userId)
	{
		$comsenzUserDao = new ComsenzUserDao();
		/*
		 * 为Discuz设置Cookie
		 * cookie计算方法参考 {discuz_home}/api/uc.php 198行: dsetcookie('auth', authcode("$member[password]\t$member[secques]\t$member[uid]", 'ENCODE', $discuz_auth_key), $cookietime);
		 */
		if ($dzUserRow = $comsenzUserDao->getDzUser($userId))
		{
			setcookie(self::_getDzCookiePre() . 'auth', ComsenzAuthCode::encode($dzUserRow["password"] . "\t" . $dzUserRow["secques"] . "\t" . $dzUserRow["uid"], self::_getDzAuthKey()), null, '/', self::_getDzCookieDomain());
		}
		/*
		 * 为Uchome设置Cookie
		 * cookie计算方法参考 {uchome_home}/api/uc.php 225行: ssetcookie('auth', authcode("$member[password]\t$member[uid]", 'ENCODE'), $cookietime);
		 */
		if ($uchUserRow = $comsenzUserDao->getUchUser($userId))
		{
			setcookie(self::_getUchCookiePre() . 'auth', ComsenzAuthCode::encode($uchUserRow["password"] . "\t" . $uchUserRow["uid"], self::_getUchAuthKey()), null, '/', self::_getUchCookieDomain());
		}		
	}

	static function delAuthCookie()
	{
		Cookie::delCookie(array("name" => self::_getDzCookiePre() . 'auth', "domain" => self::_getDzCookieDomain()));
		Cookie::delCookie(array("name" => self::_getDzCookiePre() . 'sid', "domain" => self::_getDzCookieDomain()));
		Cookie::delCookie(array("name" => self::_getUchCookiePre() . 'auth', "domain" => self::_getUchCookieDomain()));
	}

	static function getAuthUserId()
	{
		/*
		 * 只需要检测discuz或者uchome任意一个产品的cookie即可
		 */
		$cookiePre = self::_getDzCookiePre();
		$authKey = self::_getDzAuthKey();
		$auth = '';
		if (isset($_COOKIE[$cookiePre . "auth"]))
		{
			$auth = ComsenzAuthCode::decode($_COOKIE[$cookiePre . "auth"], $authKey);
			if (strlen($auth))
			{
				$authArray = explode("\t", $auth);
				return $authArray[2];
			}
		}
		else if (isset($_COOKIE[$cookiePre . "sid"]))
		{
			$comsenzUserDao = new ComsenzUserDao();
			$condition["where"]["expression"] = "sid=?";
			$condition["where"]["value"] = array($_COOKIE[$cookiePre . "sid"]);
			if ($sessionRows = $comsenzUserDao->_tgDzSession->fetchRows($condition))
			{
				return $sessionRows[0]["uid"];
			}
		}
		else
		{
			return false;
		}
	}
}